var index;

//展示列表
function showTable(path, cols) {
    layui.table.render({
        elem: '#tableId',//与页面中table标签的id一致
        id: 'reloadId',
        url: path,
        cellMinWidth: 80, //全局定义常规单元格的最小宽度，layui 2.2.1 新增
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
            title: '提示',
            layEvent: 'LAYTABLE_TIPS',
            icon: 'layui-icon-tips'
        }],
        cols: cols,
        page: true,
        limit: 10,
        limits: [10, 20, 30, 40, 50]
    });
}

// 重新加载列表
function reloadTable(url) {
//监听提交
    layui.form.on('submit(searchBtn)', function (data) {
        var formData = data.field;
        layui.table.reload('reloadId', {
            where: formData,
            page: {
                curr: 1 //重新从第 1 页开始
            },
            url: url
        });
        return false;
    });
}

//添加、修改、删除
function handlingTable(delUrl) {
    //头工具栏事件
    layui.table.on('toolbar(tableBar)', function (obj) {
        var data = obj.data;
        if (obj.event === 'add') {
            index = layer.open({
                title: '添加',
                type: 1,
                area: ['50%','50%'],
                content: $('#addAndEditId'),
                cancel: function(){
                    //    右上角的关闭按钮
                    $("#addAndEditForm")[0].reset();//清空表单数据
                }
            });
        }
    });
    //监听工具条
    layui.table.on('tool(tableBar)', function (obj) {
        var data = obj.data;
        if (obj.event === 'del') {
            layui.layer.confirm('真的删除行么', function (index) {
                $.ajax({
                    type: "GET",
                    url: delUrl+'?id='+data.id,//后台接口地址
                    success: function (result) {
                        if (result.code === 0) {
                            obj.del();
                            layui.layer.close(index);
                            layui.layer.msg(result.msg, {icon: 1,time: 2000});
                        } else {  //失败
                            layui.layer.alert(result.msg, {icon: 2}, function () {
                                layui.layer.close(index);
                            });
                        }
                    }
                })
            });
        } else if (obj.event === 'edit') {
            index = layer.open({
                title: '编辑',
                type: 1,
                content: $('#addAndEditId'),
                success: function () {
                    $('#addAndEditId').setForm(data);
                    layui.form.render();  // 下拉框赋值
                },
                cancel: function(){
                    //    右上角的关闭按钮
                    $("#addAndEditForm")[0].reset();//清空表单数据
                }
            });
        }
    });
}

//提交按钮
function submitBtn(url) {
    //监听提交
    layui.form.on('submit(subBtn)', function (data) {
        $.ajax({
            type: 'POST',
            url: url,
            contentType: "application/json; charset=utf-8",//提交数据的格式为json
            data: JSON.stringify(data.field),
            success: function (result) {
                if (result.code === 0) {
                    layui.layer.msg(result.msg, {icon: 1, time: 2000});
                    //成功后刷新父界面
                    window.location.reload();
                } else {
                    layui.layer.alert(result.msg, {icon: 2, time: 2000}, function (index) {
                        layui.layer.close(index);
                    });
                }
            }
        });
        return false;
    });
}


$.fn.setForm = function (jsonValue) {
    var obj = this;
    $.each(jsonValue, function (name, ival) {
        var $oinput = obj.find("input[name=" + name + "]");
        if ($oinput.attr("type") == "checkbox") {
            if (ival !== null) {
                var checkboxObj = $("[name=" + name + "]");
                var checkArray = ival.split(";");
                for (var i = 0; i < checkboxObj.length; i++) {
                    for (var j = 0; j < checkArray.length; j++) {
                        if (checkboxObj[i].value == checkArray[j]) {
                            checkboxObj[i].click();
                        }
                    }
                }
            }
        } else if ($oinput.attr("type") == "radio") {
            $oinput.each(function () {
                var radioObj = $("[name=" + name + "]");
                for (var i = 0; i < radioObj.length; i++) {
                    if (radioObj[i].value == ival) {
                        radioObj[i].click();
                    }
                }
            });
        } else if ($oinput.attr("type") == "textarea") {
            obj.find("[name=" + name + "]").html(ival);
        } else {
            obj.find("[name=" + name + "]").val(ival);
        }
    })
};